<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/6/20
 * Time: 15:31
 */
namespace app\admin\controller;
use app\common\model\MessageTemplate;
use app\Request;
use  app\common\model\Config;
class Community
{

    public function communityList(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit + $start;
        }
        $where  = [];
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['name','like','%'.$param['name'].'%'];
        }
        if (isset($param['username']) && !empty($param['username'])) {
            $where[] = ['nickname','like','%'.$param['username'].'%'];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where[] = ['c.status','=',$param['status']];
        }
        $communityModel = new \app\common\model\Community();
        $list = $communityModel->alias('c')
            ->leftJoin('user u', 'c.uid = u.id')
            ->field('c.*,u.nickname,u.avatar')
            ->order('sort','desc')
            ->where($where)->limit($start,$limit)->select()->toArray();
        $count = $communityModel->alias('c')
            ->leftJoin('user u', 'c.uid = u.id')
            ->where($where)->limit($start,$limit)
            ->count();
        return success('社群列表',['list' => $list,'count' => $count]);
    }
    public function joinCommunityList(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit + $start;
        }
        $where  = [];
        if (isset($param['id']) && !empty($param['id'])) {
            $where [] = ['cj.community_id','=',$param['id']];
        }
        if (isset($param['join_user']) && !empty($param['join_user'])) {
            $where [] = ['uj.nickname','=','%'.$param['join_user'].'%'];
        }
        if (isset($param['create_user']) && !empty($param['create_user'])) {
            $where [] = ['u.nickname','like','%'.$param['create_user'].'%'];
        }
        $CommunityJoinModel = new \app\common\model\CommunityJoin();
        $list = $CommunityJoinModel->alias('cj')
            ->leftJoin('community c', 'cj.community_id = c.id')
            ->leftJoin('user u', 'c.uid = u.id')
            ->leftJoin('user uj', 'cj.uid = uj.id')

            ->field('cj.*,c.name, c.desc,u.nickname,c.join_number,c.allow_join_number,u.avatar,uj.nickname as join_nickname,uj.avatar as join_avatar ')
            ->where($where)
            ->limit($start,$limit)->select()->toArray();
        $count = $CommunityJoinModel->alias('c')
            ->leftJoin('user u', 'c.uid = u.id')
            ->leftJoin('community_join cj', 'cj.community_id = c.id')
            ->leftJoin('user uj', 'cj.uid = uj.id')
            ->where($where)
            ->field('c.*,u.nickname,u.avatar,uj.nickname as join_nickname,uj.avatar as ')

            ->count();
        return success('加入圈子数据',['list' => $list,'count' => $count]);
    }
    public function updateCommunityStatus(Request $request){
        $param = $request->param();
        $CommunityModel = new \app\common\model\Community();
        $data['status']  =  $param['status'];
        if (isset($param['remark']) && !empty($param['remark'])){
            $data['remark'] = $param['remark'];
        }
        $CommunityModel->where(['id' => $param['id']])->update($data);

        return  success('操作成功');
    }
    public function updateSort(Request $request){
        $param = $request->param();
        $CommunityModel = new \app\common\model\Community();
        $CommunityModel->where(['id' => $param['id']])->update(['sort'=> $param['sort']]);
        return  success('操作成功');
    }
    public function addCommunity(Request $request){
        $param  = $request->param();
        $data = [
            'name' => $param['name'],
            'desc' => $param['desc'],
            'allow_join_number' => $param['allow_join_number'],
            'img' => $param['img'],
           /* 'uid' => $request->userInfo['id'],*/
            'price' =>  $param['price'],
            'create_time' => time(),
            'head_img' => $param['head_img'],
            'status' => 2,
        ];
        $communityModel = new \app\common\model\Community();
        $communityModel->insert($data);
        return success('发布成功');
    }

    public function editCommunity(Request $request){
        $param  = $request->param();
        $data = [
            'name' => $param['name'],
            'desc' => $param['desc'],
            'allow_join_number' => $param['allow_join_number'],
            'img' => $param['img'],
            'price' =>  $param['price'],
            'create_time' => time(),
            'head_img' => $param['head_img'],
        ];
        $communityModel = new \app\common\model\Community();
        $communityModel->where(['id' => $param['id']])->update($data);
        return success('发布成功');
    }
}